package com.huijutao;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.Toolbar;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import com.huijutao.activityContent.FMItemActivity;
import com.huijutao.callback.ABSFragment;
import com.huijutao.fragment.FMFind;
import com.huijutao.fragment.FMGroup;
import com.huijutao.fragment.FMHaiTao;
import com.huijutao.fragment.FMHuiHui;
import com.huijutao.fragment.FMYouHui;
import com.huijutao.fragment.FMYouHuiHaiTaoDetail;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends ActionBarActivity implements FMYouHui.HcFragmentCallBack,FMYouHuiHaiTaoDetail.HcContentCallBack,FMHuiHui.HuiFragmentCallBack,FMHaiTao.HaiFragmentCallBack {
    private SlidingMenu slidingMenu;
    private ListView list;
    private ViewPager mVp;
    private VpAdapter mVpAdapter;
    private RadioGroup mRg;
    private long exitTime=0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //设置Toolbar
        Toolbar toolbar = (Toolbar) findViewById(R.id.main_toolbar);
        setSupportActionBar(toolbar);
        toolbar.setTitle("惠聚淘");
        /////////////////////设置ViewPager////////////////////////
        mVp = (ViewPager) findViewById(R.id.vp);
        mVpAdapter = new VpAdapter(getSupportFragmentManager());
        mVp.setAdapter(mVpAdapter);
        mVp.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

                mVp.setPageTransformer(true,new DepthPageTransformer());
            }

            @Override
            public void onPageSelected(int position) {
                RadioButton childAt = (RadioButton) mRg.getChildAt(position);
                childAt.setChecked(true);
            }
            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        /////////////////////SlidingMenu////////////////////////
        final View view1 = LayoutInflater.from(this).inflate(R.layout.sliding_menu, null);
        list = (ListView) view1.findViewById(R.id.slidingmenu_list);
        list.setAdapter(new SimpleAdapter(this, ListMenuData.getHuiHuiTitle(),
                R.layout.sliding_menu_list1,
                new String[]{"img", "text"},
                new int[]{R.id.slidingmenu_img1, R.id.slidingmenu_text1}));
        //SlidingMenu的Item的点击事件
        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                mVpAdapter.getItem(mVp.getCurrentItem()).setType(position);
                slidingMenu.toggle();
            }
        });
        slidingMenu = new SlidingMenu(MainActivity.this);
        //设置滑动方向
        slidingMenu.setMode(SlidingMenu.LEFT);
        //设置能够显示SlidingMenu的范围
        slidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_MARGIN);
        //设置菜单的宽度
        slidingMenu.setBehindWidth(450);
        //SlidingMenu滑动时的渐变程度
        slidingMenu.setFadeDegree(0.9f);
        // 把SlidingMenu附加在Activity上
        // SlidingMenu.SLIDING_WINDOW:菜单拉开后高度是全屏的
        // SlidingMenu.SLIDING_CONTENT:菜单拉开后高度是不包含Title/ActionBar的内容区域
        slidingMenu.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);
        slidingMenu.setMenu(view1);
        slidingMenu.showMenu(true);
        //关闭SlidingMenu
        slidingMenu.toggle();
        /////////////////////////RadioGroup///////////////////////////
        mRg = (RadioGroup) findViewById(R.id.main_radiogroup);
        mRg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.main_radiobutton0:
                        list.setAdapter(new SimpleAdapter(MainActivity.this,
                                ListMenuData.getHuiHuiTitle(),
                                R.layout.sliding_menu_list1,
                                new String[]{"img", "text"},
                                new int[]{R.id.slidingmenu_img1,R.id.slidingmenu_text1 }));
                        mVp.setCurrentItem(0);
                        break;
                    case R.id.main_radiobutton1:
                        list.setAdapter(new SimpleAdapter(MainActivity.this, ListMenuData.getTitleData(),
                                R.layout.sliding_menu_list1,
                                new String[]{"img", "text"},
                                new int[]{R.id.slidingmenu_img1, R.id.slidingmenu_text1}));
                        mVp.setCurrentItem(1);
                        break;
                    case R.id.main_radiobutton2:
                        list.setAdapter(new SimpleAdapter(MainActivity.this, ListMenuData.getTitleData(),
                                R.layout.sliding_menu_list1,
                                new String[]{"img", "text"},
                                new int[]{R.id.slidingmenu_img1, R.id.slidingmenu_text1}));
                        mVp.setCurrentItem(2);
                        break;
                    case R.id.main_radiobutton3:
                        list.setAdapter(new SimpleAdapter(MainActivity.this, ListMenuData.getTitleData(),
                                R.layout.sliding_menu_list1,
                                new String[]{"img", "text"},
                                new int[]{R.id.slidingmenu_img1, R.id.slidingmenu_text1}));
                        mVp.setCurrentItem(3);
                        break;
                    case R.id.main_radiobutton4:
                        list.setAdapter(new ArrayAdapter<String>(MainActivity.this,
                                R.layout.sliding_menu_list2, R.id.slidingmenu_text2,
                                ListMenuData.getTuangouTitleData()));
                        mVp.setCurrentItem(4);
                        break;
                }
            }
        });
    }
    /**
     * 惠惠列表Fragment的接口，传值调用详情页
     */
    @Override
    public void HuiSendData(String data1, String data2, String data3) {
        Intent in=new Intent(this, FMItemActivity.class);
        Bundle bundle = new Bundle();
        bundle.putString("data1", data1);
        bundle.putString("data2", data2);
        bundle.putString("data3", data3);
        bundle.putString("code", "0");
        in.putExtras(bundle);
        startActivity(in);
    }

    /**
     * HClist的回调，打开详情
     */
    @Override
    public void HcsendData(String img_path, String title, String subtitle, String go_websit, String go_url, String praise, String content) {
        Intent in=new Intent(this, FMItemActivity.class);
        Bundle bundle=new Bundle();
        bundle.putString("img_path",img_path);
        bundle.putString("title", title);
        bundle.putString("subtitle", subtitle);
        bundle.putString("go_websit", go_websit);
        bundle.putString("go_url", go_url);
        bundle.putString("praise", praise);
        bundle.putString("content", content);
        bundle.putString("code", "1");
        in.putExtras(bundle);
        startActivity(in);
    }
    /**
     * 海淘回调
     */
    @Override
    public void haiFragmentCallBack(String img_path, String title, String subtitle, String go_websit, String go_url, String praise, String content) {
        Intent in=new Intent(this, FMItemActivity.class);
        Bundle bundle=new Bundle();
        bundle.putString("img_path", img_path);
        bundle.putString("title", title);
        bundle.putString("subtitle", subtitle);
        bundle.putString("go_websit", go_websit);
        bundle.putString("go_url", go_url);
        bundle.putString("praise", praise);
        bundle.putString("content", content);
        bundle.putString("code", "2");
        in.putExtras(bundle);
        startActivity(in);
    }
    /**
     * Hc详情的回调，方法未重写
     * @param uri
     */
    @Override
    public void hcContentCallBack(Uri uri) {

    }
    /**
     * hui详情的回调，方法未重写
     */

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.action_shoucang) {
            Intent intent = new Intent();
            intent.setClass(this,ShouCangActivity.class);
            startActivity(intent);
        }else if(id==R.id.action_setting){
            Intent intent=new Intent();
            intent.setClass(this,SettingsActivity.class);
            startActivity(intent);
        }

        return true;

    }

    class VpAdapter extends FragmentPagerAdapter {
        private List<ABSFragment> list;

        public VpAdapter(FragmentManager fm) {
            super(fm);
            list = new ArrayList<>();
            list.add(new FMHuiHui());
            list.add(new FMYouHui());
            list.add(new FMHaiTao());
            list.add(new FMFind());
            list.add(new FMGroup());
        }
        @Override
        public ABSFragment getItem(int position) {
            return list.get(position);
        }

        @Override
        public int getCount() {
            return list.size();
        }
    }

    //再按一次退出
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode==KeyEvent.KEYCODE_BACK&&event.getAction()==KeyEvent.ACTION_DOWN){
            if((System.currentTimeMillis()- exitTime)>2000){
                Toast.makeText(this,"再按一次退出",Toast.LENGTH_SHORT).show();
                exitTime =System.currentTimeMillis();
            }else {
                finish();
                System.exit(0);
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    //viewPager 切换时的动画
    public class DepthPageTransformer implements ViewPager.PageTransformer {
        private  float MIN_SCALE = 0.75f;

        @SuppressLint("NewApi")
        @Override
        public void transformPage(View view, float position) {
            int pageWidth = view.getWidth();
            if (position < -1) { // [-Infinity,-1)
                // This page is way off-screen to the left.
                view.setAlpha(0);
            } else if (position <= 0) { // [-1,0]
                // Use the default slide transition when
                // moving to the left page
                view.setAlpha(1);
                view.setTranslationX(0);
                view.setScaleX(1);
                view.setScaleY(1);
            } else if (position <= 1) { // (0,1]
                // Fade the page out.
                view.setAlpha(1 - position);
                // Counteract the default slide transition
                view.setTranslationX(pageWidth * -position);
                // Scale the page down (between MIN_SCALE and 1)
                float scaleFactor = MIN_SCALE + (1 - MIN_SCALE)
                        * (1 - Math.abs(position));
                view.setScaleX(scaleFactor);
                view.setScaleY(scaleFactor);
            } else { // (1,+Infinity]
                // This page is way off-screen to the right.
                view.setAlpha(0);
            }
        }
    }

}
